﻿using System.Configuration;
using System.Data.Common;

namespace MediaBrowserSL.DAL.Contexts
{
    /// <summary>
    /// SQL Data Context
    /// </summary>
    public class SqlDataContext
    {
        /// <summary>
        /// Create Data Context
        /// </summary>
        /// <returns></returns>
        private DbConnection CreateDataContext()
        {
            DbConnection sqlConnection = null;
            try
            {
                DbProviderFactory factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["DBConnectionString"].ProviderName);

                sqlConnection = factory.CreateConnection();
                sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
                sqlConnection.Open();
            }
            catch (System.Exception ex)
            {
                throw new PersistenceException(ex.Message);
            }

            return sqlConnection;
        }

        internal static DbConnection DataContext
        {
            get
            {
                SqlDataContext context = new SqlDataContext();
                DbConnection oContexteDonnees = oContexteDonnees = context.CreateDataContext();              
                return oContexteDonnees;
            }
        }
    }
}
